var Config = {
    itemIndex: 0
}

Config.init = function () {
    layui.form.render();
    var me = this;
    this.table = new LayUITable("table", '/system/config/list', this.initColumns());
    me.table.params = {
        height: $(".dawn-content").height() - 50
    }
    me.table.setQueryParams(me.queryParams);
    me.table.handleListenToolBar = function (event, checkStatus, obj) {
        switch (event) {
            case "add":
                layui.laytpl($("#info").html()).render({}, function (html) {
                    Config.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '添加配置',
                        area: ['500px', '100%'], //宽高
                        content: html,
                        success: function () {
                            Config.add();
                        }
                    });
                });
                break;
        }
    };
    me.table.handleListenTool = function (event, data, obj) {
        switch (event) {
            case "edit":
                layui.laytpl($("#info").html()).render({}, function (html) {
                    Config.itemIndex = layer.open({
                        type: 1,
                        offset: 'r',
                        anim: 'slideLeft', // 从右往左
                        shade: 0.1,
                        shadeClose: true,
                        title: '编辑配置',
                        area: ['500px', '100%'], //宽高
                        content: html,
                        success: function () {
                            Config.update(data.id);
                        }
                    });
                });
                break;
            case "delete":
                var operation = function () {
                    $request.get("/system/config/delete", function (data) {
                        if(data.success) {
                            Dawn.success("删除成功!");
                            Config.table.refresh();
                        } else {
                            Dawn.error(data.message);
                        }
                    }).set("id", obj.data.id).start();
                };

                Dawn.deleteConfirm("是否刪除该配置?", operation);
                break;
        }
    };
    me.table.init();
    this.addEvents();
};

Config.update = function(id) {
    $request.get("/system/config/info", function(data) {
        if(data.success) {
            layui.form.val("info", data.data);
            layui.form.render();
            layui.form.on('submit(save)', function (data) {
                $request.postJson("/system/config/update", function (res) {
                    if (res.success) {
                        Dawn.success("更新成功!");
                        Config.table.refresh();
                        layui.layer.close(Config.itemIndex);
                    } else {
                        Dawn.error(res.message);
                    }
                }).setData(data.field).start();
                return false;
            });
        } else {
            Dawn.error(data.message);
        }
    }).setData({
        id: id
    }).setAsync(false).start();
};

Config.add = function() {
    layui.form.render();
    layui.form.on('submit(save)', function (data) {
        $request.postJson("/system/config/add", function (res) {
            if (res.success) {
                Dawn.success("保存成功!");
                Config.table.refresh();
                layui.layer.close(Config.itemIndex);
            } else {
                Dawn.error(res.message);
            }
        }).setData(data.field).start();
        return false;
    });
};

Config.addEvents = function () {
    var me = this;
    $("#search").click(function () {
        me.search();
    })
    $("#reset").click(function () {
        me.reset();
    });
}

Config.reset = function () {
    $("#searchKey").val('');
    layui.form.render();
    this.search();
}

Config.search = function () {
    this.table.init();
}

Config.queryParams = function () {
    var queryData = {};
    queryData['searchKey'] = $.trim($("#searchKey").val());
    return queryData;
}

Config.initColumns = function () {
    return [
        [
            {
                field: 'id',
                title: 'ID',
                align: 'center',
                hide: true
            },
            {
                field: 'name',
                title: '配置名称',
                align: 'center'
            }, {
            field: 'code',
            title: '配置编码',
            align: 'center'
        }, {
            field: 'value',
            title: '配置内容',
            align: 'center'
        }, {
            field: 'createtime',
            title: '创建时间',
            align: 'center',
            sort: true
        }, {
            field: 'updatetime',
            title: '更新时间',
            align: 'center',
            sort: true
        }, {
            fixed: 'right',
            title: '操作',
            toolbar: '#tableTool',
            align: 'center'
        }
        ]
    ];
}

$(function () {
    layui.element.init();
    Config.init();
})
